<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing csv files.
 * @package backend-roles/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Video extends Backend_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->helper('cms');
        
        $this->load->model('v_body_positions_model');
        $this->load->model('v_drivers_model');
        $this->load->model('v_environments_model');
        $this->load->model('v_foot_positions_model');
        $this->load->model('v_hand_positions_model');
        $this->load->model('v_joints_model');
        $this->load->model('v_kneeling_positions_model');
        $this->load->model('v_muscles_model');
        $this->load->model('v_planes_model');
        $this->load->model('v_speeds_model');
        $this->load->model('v_topics_model');
        $this->load->model('v_tweaks_model');
        $this->load->model('video_body_positions_model');
        $this->load->model('video_drivers_model');
        $this->load->model('video_environments_model');
        $this->load->model('video_foot_positions_model');
        $this->load->model('video_hand_positions_model');
        $this->load->model('video_joints_model');
        $this->load->model('video_kneeling_positions_model');
        $this->load->model('video_muscles_model');
        $this->load->model('video_planes_model');
        $this->load->model('video_speeds_model');
        $this->load->model('video_topics_model');
        $this->load->model('video_tweaks_model');
        $this->load->model('videos_model');

        $this->load->library('csvreader');
        $this->load->library('form_validation');
//        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small class="help-block">', '</small></div>');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');

    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {
        set_time_limit(0);
        
        $this->csvreader->separator = '|';
        $videos = $this->csvreader->parse_file('csvs/Videos.csv');

        foreach($videos as $vid)
        {
            $video                      = array();
            $video['Code']              = isset( $vid['Code'] )           ? trim( $vid['Code'] )           : NULL;
            $video['Name']              = isset( $vid['Exercise'] )       ? trim( $vid['Exercise'] )       : NULL;
            $video['Action']            = isset( $vid['Action'] )         ? trim( $vid['Action'] )         : NULL;
            $video['Rank']              = isset( $vid['Rank'] )           ? trim( $vid['Rank'] )           : NULL;
            $video['VimeoUrlHD']        = isset( $vid['VimeoURLHD'] )     ? trim( $vid['VimeoURLHD'] )     : '';
            $video['VimeoUrlSD']        = isset( $vid['VimeoURLSD'] )     ? trim( $vid['VimeoURLSD'] )     : '';
            $video['VimeoUrlLS']        = isset( $vid['VimeoURLLS'] )     ? trim( $vid['VimeoURLLS'] )     : '';
            $video['VimeoUrlMobile']    = isset( $vid['VimeoURLMobile'] ) ? trim( $vid['VimeoURLMobile'] ) : '';
            $video['DateCreated']       = date('Y-n-d H:i:s');
            
            $video_id = $this->videos_model->insert($video);

            // body position
            $body_position_text = trim( $vid['Body position'] );
            if( $body_position_text )
            {
                $body_positions = explode(',', $body_position_text);
                foreach( $body_positions as $pos )
                {
                    $pos                                        = trim( $pos );
                    $body_position                              = array();
                    $body_position['Name']                      = $pos;
                    $body_position['DateCreated']               = date('Y-n-d H:i:s');
                    $body_position_id = $this->v_body_positions_model->push( array('Name' => $pos), $body_position);

                    $video_body_position                     = array();
                    $video_body_position['VideoID']       = $video_id;
                    $video_body_position['BodyPositionID']   = $body_position_id;
                    $video_body_position['DateCreated']      = date('Y-n-d H:i:s');
                    $this->video_body_positions_model->insert($video_body_position);
                }
            }

            // driver
            $driver_text                             = trim( $vid['Driver'] );
            if( $driver_text )
            {
                $drivers = explode(',', $driver_text);
                foreach( $drivers as $drive )
                {
                    $drive                               = trim( $drive );
                    $driver                              = array();
                    $driver['Name']                      = $drive;
                    $driver['DateCreated']               = date('Y-n-d H:i:s');
                    $driver_id = $this->v_drivers_model->push( array('Name' => $drive), $driver);

                    $video_driver                     = array();
                    $video_driver['VideoID']       = $video_id;
                    $video_driver['DriverID']         = $driver_id;
                    $video_driver['DateCreated']      = date('Y-n-d H:i:s');
                    $this->video_drivers_model->insert($video_driver);
                }
            }
            
            // environment
            $environment_text                         = trim( $vid['Environment'] );
            if( $environment_text )
            {
                $environments = explode(',', $environment_text);
                foreach( $environments as $envi )
                {
                    $envi                                     = trim( $envi );
                    $environment                              = array();
                    $environment['Name']                      = $envi;
                    $environment['DateCreated']               = date('Y-n-d H:i:s');
                    $environment_id = $this->v_environments_model->push( array('Name' => $envi), $environment);

                    $video_environment                     = array();
                    $video_environment['VideoID']       = $video_id;
                    $video_environment['EnvironmentID']    = $environment_id;
                    $video_environment['DateCreated']      = date('Y-n-d H:i:s');
                    $this->video_environments_model->insert($video_environment);
                }
            }
            
            // foot position
            $foot_position_text                         = trim( $vid['Foot position'] );
            if( $foot_position_text )
            {
                $foot_positions = explode(',', $foot_position_text);
                foreach( $foot_positions as $foot_pos )
                {
                    $foot_pos                                   = trim( $foot_pos );
                    $foot_position                              = array();
                    $foot_position['Name']                      = $foot_pos;
                    $foot_position['DateCreated']               = date('Y-n-d H:i:s');
                    $foot_position_id = $this->v_foot_positions_model->push( array('Name' => $foot_pos), $foot_position);

                    $video_foot_position                     = array();
                    $video_foot_position['VideoID']       = $video_id;
                    $video_foot_position['FootPositionID']   = $foot_position_id;
                    $video_foot_position['DateCreated']      = date('Y-n-d H:i:s');
                    $this->video_foot_positions_model->insert($video_foot_position);
                }
            }
            
            // hand position
            $hand_position_text                         = trim( $vid['Hand position'] );
            if( $hand_position_text )
            {
                $hand_positions = explode(',', $hand_position_text);
                foreach( $hand_positions as $hand_pos )
                {
                    $hand_pos                                   = trim( $hand_pos );
                    $hand_position                              = array();
                    $hand_position['Name']                      = $hand_pos;
                    $hand_position['DateCreated']               = date('Y-n-d H:i:s');
                    $hand_position_id = $this->v_hand_positions_model->push( array('Name' => $hand_pos), $hand_position);

                    $video_hand_position                     = array();
                    $video_hand_position['VideoID']       = $video_id;
                    $video_hand_position['HandPositionID']   = $hand_position_id;
                    $video_hand_position['DateCreated']      = date('Y-n-d H:i:s');
                    $this->video_hand_positions_model->insert($video_hand_position);
                }
            }
            
            // joint
            $joint_text                         = trim( $vid['Joint'] );
            if( $joint_text )
            {
                $joints = explode(',', $joint_text);
                foreach( $joints as $jnt )
                {
                    $jnt                                = trim( $jnt );
                    $joint                              = array();
                    $joint['Name']                      = $jnt;
                    $joint['DateCreated']               = date('Y-n-d H:i:s');
                    $joint_id = $this->v_joints_model->push( array('Name' => $jnt), $joint);

                    $video_joint                     = array();
                    $video_joint['VideoID']       = $video_id;
                    $video_joint['JointID']          = $joint_id;
                    $video_joint['DateCreated']      = date('Y-n-d H:i:s');
                    $this->video_joints_model->insert($video_joint);
                }
            }
            
            // kneeling position
            $kneeling_position_text                         = trim( $vid['Kneeling position'] );
            if( $kneeling_position_text )
            {
                $kneeling_positions = explode(',', $kneeling_position_text);
                foreach( $kneeling_positions as $kneel_pos )
                {
                    $kneel_pos                                      = trim( $kneel_pos );
                    $kneeling_position                              = array();
                    $kneeling_position['Name']                      = $kneel_pos;
                    $kneeling_position['DateCreated']               = date('Y-n-d H:i:s');
                    $kneeling_position_id = $this->v_kneeling_positions_model->push( array('Name' => $kneel_pos), $kneeling_position);

                    $video_kneeling_position                         = array();
                    $video_kneeling_position['VideoID']           = $video_id;
                    $video_kneeling_position['KneelingPositionID']   = $kneeling_position_id;
                    $video_kneeling_position['DateCreated']          = date('Y-n-d H:i:s');
                    $this->video_kneeling_positions_model->insert($video_kneeling_position);
                }
            }
            
            // muscle
            $muscle_text                         = trim( $vid['Muscles'] );
            if( $muscle_text )
            {
                $muscles = explode(',', $muscle_text);
                foreach( $muscles as $mscl )
                {
                    $mscl                                = trim( $mscl );
                    $muscle                              = array();
                    $muscle['Name']                      = $mscl;
                    $muscle['DateCreated']               = date('Y-n-d H:i:s');
                    $muscle_id = $this->v_muscles_model->push( array('Name' => $mscl), $muscle);

                    $video_muscle                     = array();
                    $video_muscle['VideoID']       = $video_id;
                    $video_muscle['MuscleID']         = $muscle_id;
                    $video_muscle['DateCreated']      = date('Y-n-d H:i:s');
                    $this->video_muscles_model->insert($video_muscle);
                }
            }
            
            // plane
            $plane_text                             = trim( $vid['Plane'] );
            if( $plane_text )
            {
                $planes = explode(',', $plane_text);
                foreach( $planes as $pln )
                {
                    $pln                                = trim( $pln );
                    $plane                              = array();
                    $plane['Name']                      = $pln;
                    $plane['DateCreated']               = date('Y-n-d H:i:s');
                    $plane_id = $this->v_planes_model->push( array('Name' => $pln), $plane);

                    $video_plane                     = array();
                    $video_plane['VideoID']       = $video_id;
                    $video_plane['PlaneID']          = $plane_id;
                    $video_plane['DateCreated']      = date('Y-n-d H:i:s');
                    $this->video_planes_model->insert($video_plane);
                }
            }
            
            // speed
            $speed_text                             = trim( $vid['Speed'] );
            if( $speed_text )
            {
                $speeds = explode(',', $speed_text);
                foreach( $speeds as $spd )
                {
                    $spd                                = trim( $spd );
                    $speed                              = array();
                    $speed['Name']                      = $spd;
                    $speed['DateCreated']               = date('Y-n-d H:i:s');
                    $speed_id = $this->v_speeds_model->push( array('Name' => $spd), $speed);

                    $video_speed                     = array();
                    $video_speed['VideoID']       = $video_id;
                    $video_speed['SpeedID']          = $speed_id;
                    $video_speed['DateCreated']      = date('Y-n-d H:i:s');
                    $this->video_speeds_model->insert($video_speed);
                }
            }
            
            // tweak
            $tweak_text                         = trim( $vid['Tweaking'] );
            if( $tweak_text )
            {
                $tweaks = explode(',', $tweak_text);
                foreach( $tweaks as $twk )
                {
                    $twk                                = trim( $twk );
                    $tweak                              = array();
                    $tweak['Name']                      = $twk;
                    $tweak['DateCreated']               = date('Y-n-d H:i:s');
                    $tweak_id = $this->v_tweaks_model->push( array('Name' => $twk), $tweak);

                    $video_tweak                     = array();
                    $video_tweak['VideoID']       = $video_id;
                    $video_tweak['TweakID']          = $tweak_id;
                    $video_tweak['DateCreated']      = date('Y-n-d H:i:s');
                    $this->video_tweaks_model->insert($video_tweak);
                }
            }
            
            // topic
            $topic_text                             = trim( $vid['Topic'] );
            if( $topic_text )
            {
                $topics = explode(',', $topic_text);
                foreach( $topics as $tpc )
                {
                    $tpc                                = trim( $tpc );
                    $topic                              = array();
                    $topic['Name']                      = $tpc;
                    $topic['Code']                      = isset( $vid['TopicCode'] ) ? trim( $vid['TopicCode'] ) : NULL;
                    $topic['DateCreated']               = date('Y-n-d H:i:s');
                    $topic_id = $this->v_topics_model->push( array('Name' => $tpc), $topic);

                    $video_topic                     = array();
                    $video_topic['VideoID']          = $video_id;
                    $video_topic['TopicID']          = $topic_id;
                    $video_topic['DateCreated']      = date('Y-n-d H:i:s');
                    $this->video_topics_model->insert($video_topic);
                }
            }
        }

        echo '<pre>';
        print_r($videos); 
        echo '</pre>';
        exit();
        
    }
}

/* End of file parsecsv.php */
/* Location: ./application/roles/backend-csvreader/controllers/parsecsv.php */